package final2107.day09;

import java.util.Scanner;

public class ThreeSplitDemo {
    public static void main(String[] args) {
        System.out.println("请输入数字字符串:");
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        String dest = threeSplit(str);
        System.out.println(dest);
    }

    //定义方法完成三位分节法
    public static String threeSplit(String input){
        StringBuilder builder = new StringBuilder(input);
        builder.reverse();

        //将字符串转换为字符数组
        char[] ary = builder.toString().toCharArray();
        //将StringBuilder清空,用于后续保存拼接的字符
        builder.delete(0,builder.length());

        for (int i=0;i<ary.length;i++){
            builder.append(ary[i]);
            if ((i+1)%3==0 && i!=ary.length-1)
                builder.append(",");
        }
        return builder.reverse().toString();

    }
}
